package top.frsf.object.bullet;

import top.frsf.object.GameObject;
import top.frsf.object.zombie.ZomObject;
import top.frsf.panel.GamePanel;
import top.frsf.starter.impl.BulletStarter;
import top.frsf.starter.impl.ZomStarter;

import java.awt.*;

import static top.frsf.utils.ConfigLoader.getInt;

/**
 * 子弹基类
 *
 * @author FRSF
 * @since 2025/8/3 13:56
 */
public abstract class BulletObject extends GameObject {
    /**
     * 子弹速度
     */
    protected int speed;
    /**
     * 子弹攻击力
     */
    protected int attackHp;

    /**
     * 构造函数
     *
     * @param img      图片路径
     * @param x        x坐标
     * @param y        y坐标
     * @param width    宽度
     * @param height   高度
     * @param speed    子弹速度
     * @param attackHp 子弹攻击力
     */
    public BulletObject(String img, int x, int y, int width, int height, int speed, int attackHp) {
        super(img, x, y, width, height);
        this.speed = speed;
        this.attackHp = attackHp;
    }


    @Override
    public void paintSelfImage(Graphics g) {
        super.paintSelfImage(g);
        move();
        destroy();
    }

    /**
     * 子弹移动
     */
    protected void move() {
        setX(getX() + speed);
    }

    /**
     * 子弹攻击行为
     *
     * @param zomObject 攻击对象
     */
    protected void attack(ZomObject zomObject) {
        zomObject.downHp(attackHp);
    }

    /**
     * 子弹销毁
     */
    protected void destroy() {
        BulletStarter bulletStarter = GamePanel.starterContext.getStarter(BulletStarter.class);
        ZomStarter zomStarter = GamePanel.starterContext.getStarter(ZomStarter.class);
        // 子弹越界
        if (getX() > getInt("game.width")) {
            bulletStarter.getDelBulls().add(this);
            return;
        }
        // 子弹击中zom
        for (ZomObject zomObject : zomStarter.getZomList()) {
            if (zomObject.getRectangle().intersects(getRectangle())) {
                attack(zomObject);
                bulletStarter.getDelBulls().add(this);
                return;
            }
        }
    }
}
